package com.example.reagent_management.ui.operatedialog.viewmodle

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.reagent_management.room.entity.HistoryInfo
import com.example.reagent_management.room.entity.OperateInfoQuery
import com.example.reagent_management.room.entity.ReagentInfoQuery
import com.example.reagent_management.ui.operatedialog.repository.HistoryRepository
import com.example.reagent_management.ui.operatedialog.repository.ReagentRepository
import com.example.reagent_management.ui.operatedialog.repository.StoreRepository
import com.kunminx.architecture.ui.callback.UnPeekLiveData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class BottomHistoryViewModel @Inject constructor(
    private val reagentRepository: ReagentRepository,
    private val storeRepository: StoreRepository,
    private val historyRepository: HistoryRepository,
) : ViewModel() {

    val history = UnPeekLiveData<HistoryInfo>()
    val reagent = UnPeekLiveData<ReagentInfoQuery>()
    val operate = UnPeekLiveData<OperateInfoQuery>()
    val success = UnPeekLiveData<Boolean>()

    fun setAllData() {

        history.value?.let {
            viewModelScope.launch(Dispatchers.IO) {
                async {
                    reagent.postValue(reagentRepository.queryReagentById(it.reagent_id))
                }
                async {
                    operate.postValue(storeRepository.getSupplierAndNum(it.operate_id))
                }
            }
        }
    }

    fun rollBack() {
        history.value?.let {
            viewModelScope.launch(Dispatchers.IO) {
                //出入库数量
                val operateInfo = storeRepository.getOperateInfo(it.operate_id)
                val lastAllCounts = storeRepository.getLastCounts(it.batch, it.reagent_id)
                if (operateInfo == null || lastAllCounts == null) {
                    success.postValue(false)
                    return@launch
                }
                //出入库数量
                val num = operateInfo.num
                if (it.operateType) {
                    //入库 简单化 复杂的就是一堆if else 判断
                    storeRepository.deleteById(it.operate_id)

                    if (lastAllCounts.operate_id != it.operate_id) {
                        //更新总数
                        storeRepository.updateLastCounts(
                            lastAllCounts.operate_id,
                            lastAllCounts.counts - num
                        )
                        //更新历史总库存
                        historyRepository.updateCounts(
                            it.batch,
                            it.reagent_id,
                            lastAllCounts.counts - num
                        )
                    } else {
                        historyRepository.updateCounts(it.batch, it.reagent_id, 0)
                    }
                } else {
                    //出库
                    val inStoreNum = storeRepository.getNumById(operateInfo.counts)
                    //更细入库id的num
                    storeRepository.updateInStore(inStoreNum + num, operateInfo.counts)
                    //更细总数
                    storeRepository.updateLastCounts(
                        lastAllCounts.operate_id,
                        lastAllCounts.counts + num
                    )
                    //更新历史总库存
//                        historyRepository.updateCounts(
//                            it.batch,
//                            it.reagent_id,
//                            lastAllCounts.counts + num
//                        )
                    storeRepository.deleteById(it.operate_id)
                }

                val result = historyRepository.deleteById(it.history_id)
                if (result != -1) {
                    success.postValue(true)
                } else {
                    //重新插入 id也变了 逻辑要改
//                    storeRepository.insertOperate(operateInfo)
                    success.postValue(false)
                }
            }
        }
    }

}